
from generateboard import *
import turtle
from boardUpdate import *
import time


def drawingboard(Ninput):

    global t
    global tNum
    global tPlayer
    global boardSet
    global N

    N = Ninput

    boardSet = generateboard([0.05, 0.15, 0.8], N)
    board = boardSet[0]

    #for key in board.keys():
    #    print(key)

    n = 40  # 每行间隔
    x = -300  # x初始值
    y = -300  # x初始值
    t = turtle.Turtle(visible=False)
    tNum = turtle.Turtle(visible=False)
    tPlayer = turtle.Turtle(visible=False)
    #turtle.Turtle().screen.delay(0)
    #t.tracer(0)
    turtle.tracer(False)
    t.pensize(2)

    # 先画12*12的正方形，并按要求涂黑
    for i in range(N):
        for j in range(1, N+1):
            t.penup()
            t.goto(x + i * n, y + j * n)
            t.pendown()
            if board[(i+1,j)][0] != 2:
                for index in range(4):
                    t.forward(n)
                    t.left(90)
                t.penup()
            else:
                t.begin_fill()
                t.fillcolor('black')
                for index in range(4):
                    t.forward(n)
                    t.left(90)
                t.end_fill()
            t.penup()


    for i in range(N):
        for j in range(1,N+1):
            if board[(i+1,j)][0] == 1:
                tNum.penup()
                tNum.goto(x + i * n + n/3.0, y + j * n + n/3.0)
                tNum.pendown()
                tNum.color('gold')
                tNum.write(str(board[(i+1,j)][1]), font=("Times", 14, "bold"))
                tNum.color('black')
                tNum.penup()
                tNum.goto(x + i * n, y + j * n)
            elif board[(i+1,j)][0] == 4:
                if board[(i+1,j)][1] == 1:
                    tPlayer.penup()
                    tPlayer.goto(x + i * n + n/2.0, y + j * n + n/3.5)
                    tPlayer.pendown()
                    tPlayer.begin_fill()
                    tPlayer.color('red')
                    tPlayer.circle(10)
                    tPlayer.end_fill()
                    tPlayer.color('black')
                    tPlayer.penup()
                elif board[(i+1,j)][1] == 2:
                    tPlayer.penup()
                    tPlayer.goto(x + i * n + n/2.0, y + j * n + n/3.5)
                    tPlayer.pendown()
                    tPlayer.begin_fill()
                    tPlayer.color('blue')
                    tPlayer.circle(10)
                    tPlayer.end_fill()
                    tPlayer.color('black')
                    tPlayer.penup()


    # 再画外面一个正方形
    x1 = x - n * 0.12
    y1 = y - n * 0.12 + n
    t.goto(x1, y1)
    t.pendown()
    t.pensize(4)
    for index in range(4):
        t.forward(n * N + 2 * n * 0.12)
        t.left(90)
    t.penup()


    ##画分数版
    for i in range(2):
        if boardSet[2][i][0] == 1:
            x1 = x + (N+1)*n
            y1 = y + (N/2.0)*n + n * 0.1
            t.goto(x1, y1)
            t.pendown()
            t.begin_fill()
            t.color('red')
            t.circle(10)
            t.end_fill()
            t.penup()

            x1 = x +(N+1)*n + 1*n
            y1 = y +(N/2.0)*n + n * 0.1
            tNum.penup()
            tNum.goto(x1, y1)
            tNum.pendown()
            tNum.color('gold')
            tNum.write(str(boardSet[2][i][1]), font=("Times", 18, "bold"))
            tNum.color('black')
            tNum.penup()
        elif boardSet[2][i][0] == 2:
            x1 = x + (N+1)*n
            y1 = y + (N/2.0)*n - n * 0.6
            t.goto(x1, y1)
            t.pendown()
            t.begin_fill()
            t.color('blue')
            t.circle(10)
            t.end_fill()
            t.penup()

            x1 = x +(N+1)*n + 1 * n
            y1 = y +(N/2.0)*n - n * 0.6
            tNum.penup()
            tNum.goto(x1, y1)
            tNum.pendown()
            tNum.color('gold')
            tNum.write(str(boardSet[2][i][1]), font=("Times", 18, "bold"))
            tNum.color('black')
            tNum.penup()


# def moveBef():
#     global N
#     n = 40  # 每行间隔
#     x = -300  # x初始值
#     y = -300  # x初始值
#     turtle.tracer(False)
#     #turtle.Turtle().screen.delay(0)
#     for i in range(2):
#         I = boardSet[1][i][0]
#         J = boardSet[1][i][1]
#         t.penup()
#         t.goto(x + (I - 1) * n + n/2.0, y + J * n + n/4)
#         t.pendown()
#         t.begin_fill()
#         t.color('white')
#         t.circle(13)
#         t.end_fill()
#         t.color('black')
#         t.penup()
#
#     for i in range(N):
#         for j in range(1,N+1):
#             if boardSet[0][(i+1,j)][0] == 1:
#                 t.penup()
#                 t.goto(x + i * n + n/3.0, y + j * n + n/3.0)
#                 t.pendown()
#                 t.color('white')
#                 t.write(str(boardSet[0][(i+1,j)][1]), font=("Times", 14, "bold"))
#                 t.color('black')
#                 t.penup()
#
#     turtle.tracer(True)


def moveBef():
    global t
    global tNum
    global tPlayer

    turtle.tracer(False)
    tNum.clear()
    tPlayer.clear()
    turtle.tracer(False)

# def moveAft():
#     n = 40  # 每行间隔
#     x = -300  # x初始值
#     y = -300  # x初始值
#     global boardSet
#     turtle.tracer(False)
#     #turtle.Turtle().screen.delay(0)
#     for i in range(2):
#         I = boardSet[1][i][0]
#         J = boardSet[1][i][1]
#
#         t.penup()
#         t.goto(x + (I - 1) * n + n / 2.0, y + J * n + n / 4)
#         t.pendown()
#         t.begin_fill()
#         t.color('white')
#         t.circle(13)
#         t.end_fill()
#         t.color('black')
#         t.penup()
#         t.goto(x + (I - 1) * n + n / 2.0, y + J * n + n/3.5)
#         t.pendown()
#         t.begin_fill()
#         if boardSet[1][i][3] == 1:
#             t.color('red')
#             print([I,J])
#         elif boardSet[1][i][3] == 2:
#             t.color('blue')
#         t.circle(10)
#         t.end_fill()
#         t.color('black')
#         t.penup()
#
#     for i in range(2):
#         if boardSet[2][i][0] == 1:
#             x1 = x + n*(N+1) +1*n
#             y1 = y + n*(N/2.0) +n * 0.1
#
#             t.penup()
#             t.goto(x1, y1)
#             t.pendown()
#             t.color('white')
#             t.write(str(boardSet[2][i][2]), font=("Times", 18, "bold"))
#             t.color('black')
#             t.penup()
#
#             t.goto(x1, y1)
#             t.pendown()
#             t.color('gold')
#             t.write(str(boardSet[2][i][1]), font=("Times", 18, "bold"))
#             t.color('black')
#             t.penup()
#
#         elif boardSet[2][i][0] == 2:
#
#             x1 = x + (N+1)*n+ 1 * n
#             y1 = y + (N/2.0)*n - n * 0.6
#
#             t.penup()
#             t.goto(x1, y1)
#             t.pendown()
#             t.color('white')
#             t.write(str(boardSet[2][i][2]), font=("Times", 18, "bold"))
#             t.color('black')
#             t.penup()
#
#             t.goto(x1, y1)
#             t.pendown()
#             t.color('gold')
#             t.write(str(boardSet[2][i][1]), font=("Times", 18, "bold"))
#             t.color('black')
#             t.penup()
#
#     for i in range(N):
#         for j in range(1,N+1):
#             if boardSet[0][(i+1,j)][0] == 1:
#                 t.penup()
#                 t.goto(x + i * n + n/3.0, y + j * n + n/3.0)
#                 t.pendown()
#                 t.color('gold')
#                 t.write(str(boardSet[0][(i+1,j)][1]), font=("Times", 14, "bold"))
#                 t.color('black')
#                 t.penup()
#
#     turtle.tracer(True)


def moveAft():
    n = 40  # 每行间隔
    x = -300  # x初始值
    y = -300  # x初始值
    global boardSet
    turtle.tracer(False)
    #turtle.Turtle().screen.delay(0)
    #tPlayer.pendown()
    for i in range(2):
        I = boardSet[1][i][0]
        J = boardSet[1][i][1]
        tPlayer.penup()
        tPlayer.goto(x + (I - 1) * n + n / 2.0, y + J * n + n / 4)
        tPlayer.pendown()
        tPlayer.begin_fill()
        if boardSet[1][i][3] == 1:
            tPlayer.color('red')
            print([I,J])
        elif boardSet[1][i][3] == 2:
            tPlayer.color('blue')
        tPlayer.circle(10)
        tPlayer.end_fill()
        tPlayer.color('black')
        tPlayer.penup()

    for i in range(2):
        if boardSet[2][i][0] == 1:
            x1 = x + n*(N+1) +1*n
            y1 = y + n*(N/2.0) +n * 0.1
            # t.penup()
            # t.goto(x1, y1)
            # t.pendown()
            # t.color('white')
            # t.write(str(boardSet[2][i][2]), font=("Times", 18, "bold"))
            # t.color('black')
            # t.penup()

            tNum.goto(x1, y1)
            tNum.pendown()
            tNum.color('gold')
            tNum.write(str(boardSet[2][i][1]), font=("Times", 18, "bold"))
            tNum.color('black')
            tNum.penup()

        elif boardSet[2][i][0] == 2:

            x1 = x + (N+1)*n+ 1 * n
            y1 = y + (N/2.0)*n - n * 0.6

            # t.penup()
            # t.goto(x1, y1)
            # t.pendown()
            # t.color('white')
            # t.write(str(boardSet[2][i][2]), font=("Times", 18, "bold"))
            # t.color('black')
            # t.penup()
            tNum.penup()
            tNum.goto(x1, y1)
            tNum.pendown()
            tNum.color('gold')
            tNum.write(str(boardSet[2][i][1]), font=("Times", 18, "bold"))
            tNum.color('black')
            tNum.penup()

    for i in range(N):
        for j in range(1,N+1):
            if boardSet[0][(i+1,j)][0] == 1:
                tNum.penup()
                tNum.goto(x + i * n + n/3.0, y + j * n + n/3.0)
                tNum.pendown()
                tNum.color('gold')
                tNum.write(str(boardSet[0][(i+1,j)][1]), font=("Times", 14, "bold"))
                tNum.color('black')
                tNum.penup()

    turtle.tracer(True)


def gaming():
    global boardSet

    #time.sleep(2)
    moveBef()
    turtle.tracer(False)
    turtle.Turtle().screen.delay(0.5)
    boardSet = gamingBoard(boardSet)
    turtle.ontimer(gaming, 700)
    moveAft()



def main():

    turtle.tracer(False)

    ###设定棋盘是N*N
    drawingboard(12)
    turtle.tracer(True)
    gaming()

    turtle.mainloop()

if __name__ == "__main__":
    main()




